package com.lvjian.community.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.lvjian.community.service.UserRoleService;
import com.lvjian.community.utils.Consts;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户角色Controller
 */
@RestController
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;

    @RequestMapping(value = "/user/role", method = RequestMethod.GET)
    @ResponseBody
    public Object userRoles(HttpServletRequest request, HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        List<String> roles = userRoleService.obtainUserRoles();

        try {
            if (!roles.isEmpty()) {
                jsonObject.put(Consts.CODE, 1);
                jsonObject.put("roles", roles);
            } else {
                jsonObject.put(Consts.CODE, 0);
            }
        } catch (Exception e) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, e.toString());
        }

        return jsonObject;
    }

}
